import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {CompanyService} from './company.service';
import {BaseComponent} from '../common/base.component';
import {AddCompanyPage, AddCompany} from './company';

declare var $: any;

@Component({
  selector: 'app-company',
  templateUrl: './company.component.html'
})
export class CompanyComponent extends BaseComponent implements OnInit {
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  addCompanyPage: AddCompanyPage;
  addCompany: AddCompany[];
  curaddCompany: AddCompany;
  companyId: String = '';

  constructor(private companyService: CompanyService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddCompany = {
      name: '',
      companyId: '',
      contacts: '',
      phone: '',
      remark: '',
      sign: '',
      address: '',
      createTime: ''
    };
    this.searchUser();
  }

// 新增
  onAddCompany() {
    this.isAddEditor = true;
    this.editorTitle = '新增公司';
    this.curaddCompany = {
      name: '',
      companyId: '',
      contacts: '',
      phone: '',
      remark: '',
      sign: '',
      address: '',
      createTime: ''
    };
    $('#data_editor').modal('show');
  }

  // 修改
  onEditCompany(value: AddCompany) {
    this.isAddEditor = false;
    this.editorTitle = '修改公司';
    this.curaddCompany = value;
    this.companyId = this.curaddCompany.companyId;
    $('#data_editor').modal('show'); // 显示编辑对话框
  }

  // 新增/修改
  save() {
    if (this.isAddEditor) { // 新增数据
      this.curPage = 1;
      if (this.curaddCompany.name === '') {
        return;
      } else {
        this.companyService.addCompanyInfo(this.curaddCompany)
          .then(result => {
            if (result) {
              this.alert.info('新增成功');
              this.queryList();
            } else {
              this.alert.error('新增失败');
            }
          });
      }
    } else { // 修改数据
      this.companyService.updateCompanyinfo(this.curaddCompany, this.companyId)
        .then(result => {
          if (result) {
            this.alert.info('修改成功');
            this.queryList();
          } else {
            this.alert.error('修改失败');
          }
        });
    }
    $('#data_editor').modal('hide');
  }

  // 查询公司
  searchUser() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

// 删除
  delete() {
    this.companyService.removeUserinfo(this.curaddCompany)
      .then(
        res => {
          if (res) {
            this.alert.info('删除成功');
            this.queryList();
          } else {
            this.alert.error('删除失败');
          }
        });
    $('#delete_confirm').modal('hide');
  }

  // 删除
  onDeleteCompany(value: AddCompany) {
    this.curaddCompany = value;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

  private queryList() {
    this.companyService.getCompany(this.curPage)
      .then(page => {
          this.addCompanyPage = page;
          this.addCompany = page.addCompany.datas;
        }
      );
  }
}
